package org.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.io.OutputStream;

public class Demo04WriteFile {
    public static void main(String[] args) {
        String server = "hdfs://192.168.56.102:8020"; // HDFS 的 URI
        System.setProperty("HADOOP_USER_NAME", "hadoop"); // 设置 HDFS 用户

        Configuration config = new Configuration();
        config.set("fs.defaultFS", server);

        try (FileSystem fs = FileSystem.get(config)) {
            Path filePath = new Path("/test/b.txt"); // 文件路径
            try (OutputStream out = fs.create(filePath)) {
                out.write("Hello Hadoop\n".getBytes());
                out.write("中文写入测试\n".getBytes());
                // No need to explicitly close OutputStream, it's handled by try-with-resources
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}